﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using com.codeplex.eatoutadvocate.Web.Mvc.Models;
using System.Web.Routing;
using com.codeplex.eatoutadvocate.Web.Mvc.Services;
using com.codeplex.eatoutadvocate.Web.Mvc.Utility;

namespace EatOutAdvocate.Web.Mvc.Controllers
{
    [Authorize]
    public class RecordController : Controller
    {
        //private string sessionId;
        private EatOutAdvocateService _recordService { get; set; }
        private LocationService _locationService { get; set; }
        private WeatherService _weatherService { get; set; }
        protected override void Initialize(RequestContext requestContext)
        {
            if (_recordService == null)
                _recordService = new EatOutAdvocateService();

            if (_locationService == null)
                _locationService = new LocationService();

            if (_weatherService == null)
                _weatherService = new WeatherService();

            base.Initialize(requestContext);

            /*
            if (Request.IsAuthenticated)
            {
                System.Web.SessionState.HttpSessionState ss = System.Web.HttpContext.Current.Session;
                sessionId = ss.SessionID;
                //userId = (UserData as CustomPrincipal).UserId;
                //isUser = userRole != null ? true : false;
            }
            else
            {
                var authenticate = new FormsAuthenticationService();
                authenticate.SignOut();
            }
            */
        }

        //
        // GET: /Record/

        public ActionResult Index()
        {
            //New view model
            EatOutModel eo = new EatOutModel();
            return View(eo);
        }

        [HttpPost]
        public ActionResult Index(EatOutModel eo)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    long userId = -1;
                    long.TryParse(Session["FacebookUserId"].ToString(), out userId);
                    if (-1 == userId)
                    {
                        // TODO: Log user out
                    }
                    string appkey = ConfigurationManager.AppSettings["WeatherAppKey"].ToString();
                    string zipcode = eo.ZipCode;
                    _weatherService.Populate(zipcode, appkey);
                    eo.Weather = (int)_weatherService.GetWeather();
                    eo.Temperature = (int)_weatherService.GetTemperature();
                    eo.Time = DateTime.Now.Hour;
                    _recordService.Store(userId, eo);
                    ViewBag.Message = "Your Eat Out Data Has Been Successfully Recorded. Thank You.";
                    return View("Success");
                }
                catch (Exception exp)
                {
                    LogUtility.ProcessException("Record", exp.Message.ToString());
                    return View("~/Views/Shared/Error.cshtml");
                }
            }
            else
            {
                return View(eo);
            }
        }

        /// <summary>
        /// Get the list of restaurants
        /// </summary>
        /// <returns>Json list of restaurants</returns>
        [HttpPost]
        public JsonResult GetRestaurants(string term)
        {
            //Get restaurants from database
            List<string> restaurants = _recordService.GetRestaurants(term);
            return Json(restaurants);
        }
    }
}
